#include "inputCommand.h"
#include "msg_queue.h"
#include <wiringSerial.h>
#include <wiringPi.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define SMOKE_PIN 6
#define SMOKE_MODE INPUT

int smokeInit(struct inputCommand *command, char *name, char *ipAddress, int port);
int smokeGetCommand(struct inputCommand *command, char *cmd);

struct inputCommand smokeCommand = {
    .commandName = "smokeDevice",
    .command = {'\0'},
    .Init = smokeInit,
    .getCommand = smokeGetCommand,
    .log = {'\0'}};

int smokeInit(struct inputCommand *command, char *name, char *ipAddress, int port)
{
    pinMode(SMOKE_PIN, SMOKE_MODE);
    return -1;
}

int smokeGetCommand(struct inputCommand *command, char *cmd)
{
    int nread = 0;
    int status = HIGH;
    int smoke_signal = 0;
    memset(command->command, '\0', sizeof(command->command));
    while (1)
    {
        status = digitalRead(SMOKE_PIN);
        if (status == LOW)
        {
            smoke_signal = 1;
        }
        else if (status == HIGH && smoke_signal == 1)
        {
            smoke_signal = 0;
        }
    }

    return nread;
}

struct inputCommand *addsmokeToInputCommandLink(struct inputCommand *phead)
{
    if (phead == NULL)
    {
        return &smokeCommand;
    }
    else
    {
        smokeCommand.Next = phead;
        phead = &smokeCommand;
        return phead;
    }
}
